<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="formRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <!-- 日期选择组件 -->
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input :value="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" />
        <el-tree v-if="showTree" v-loading="loading" class="tree-box" :data="treeData" :props="{ label: 'name' }" @node-click="nodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <!-- 日期选择组件 -->
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { transArray2Tree } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{
          required: true,
          message: '用户姓名不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 4,
          message: '用户姓名为1-4位'
        }
        ],
        mobile: [{
          required: true,
          message: '手机号不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }
        ],
        formOfEmployment: [{
          required: true,
          message: '聘用形式不能为空',
          trigger: 'blur'
        }],
        workNumber: [{
          required: true,
          message: '工号不能为空',
          trigger: 'blur'
        }],
        departmentName: [{
          required: true,
          message: '部门不能为空',
          trigger: 'change'
        }],
        timeOfEntry: [{
          required: true,
          message: '入职时间',
          trigger: 'blur'
        }]
      },
      treeData: [],
      showTree: false,
      loading: false
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeData = transArray2Tree(depts, '')
      this.loading = false
    },
    nodeClick(item) {
      // 隐藏弹框
      this.showTree = false
      // item 就是当前点击行的数据，把当前点击项的 name 给 input 框
      this.formData.departmentName = item.name
    },
    btnOK() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          // 调用新增接口
          await addEmployee(this.formData)
          // 前端重新获取新的数据
          // this.$emit('addEmp')
          // !this.$parent 可以获取父组件的实例
          this.$parent.getEmployeeList()
          // 关闭弹框
          // this.$emit('update:showDialog', false)
          this.$parent.showDialog = false
          // 提示新增成功
          this.$message.success('操作成功')
        }
      })
    },
    closeDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.showTree = false
      this.$refs.formRef.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>
<style lang="scss" scoped>
    .tree-box {
        position: absolute;
        width: 50%;
        min-height: 50px;
        left: 0;
        top: 45px;
        z-index: 100;
        border: 1px solid #ccc;
        border-radius: 4px;
        padding-right: 5px;
        overflow: hidden;
        background-color: #fff;
        max-height: 200px;
        overflow: auto;

        ::v-deep {
            .el-tree-node__content {
                height: auto;
            }
        }
        /* & 就表示父亲的选择器，这里就是 .tree-box */
        &::-webkit-scrollbar {
            width: 10px;
        }

        &::-webkit-scrollbar-track {
            box-shadow: inset 0 0 6px #EAEAEA;
            border-radius: 10px;
        }

        &::-webkit-scrollbar-track-piece {
            background-color: rgba(233, 233, 233, 1);
            box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
            border-radius: 5px;
        }

        &::-webkit-scrollbar-thumb {
            border-radius: 10px;
            background: #515151;
            box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
        }
    }
</style>
